package com.syf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.syf.domain.Coin;
import com.syf.service.CoinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syf.mapper.CoinConfigMapper;
import com.syf.domain.CoinConfig;
import com.syf.service.CoinConfigService;
/**
*
*
*@author 苏耀奋
*@create 2021-10-12 18:48
*/
@Service
public class CoinConfigServiceImpl extends ServiceImpl<CoinConfigMapper, CoinConfig> implements CoinConfigService{

    @Autowired
    private CoinService coinService;


    /**
     * 根据币种Id 查询币种的配置信息
     * @param coinId
     * @return
     */
    @Override
    public CoinConfig findByCoinId(Long coinId) {
        return getOne(new LambdaQueryWrapper<CoinConfig>().eq(coinId != null, CoinConfig::getId, coinId) );
    }


    /**
     * 币种配置的修改操作
     * @param coinConfig
     * @return
     */
    @Override
    public boolean updateOrSave(CoinConfig coinConfig) {
        Coin coin = coinService.getById(coinConfig.getId()); //查的是 coin, 非coinConfig
        if(coin == null){
            throw new IllegalArgumentException("该币种Id不存在！");
        }
        coinConfig.setCoinType(coin.getType());
        coinConfig.setName(coin.getName());

        // 如何是新增/修改呢?   查询ID
        CoinConfig config = getById(coinConfig.getId());
        if (config == null) { // 新增操作
            return save(coinConfig);
        } else { // 修改操作
            return updateById(coinConfig);
        }
    }
}
